﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using Microsoft.Office.Interop.Excel;

namespace WFNetLib.Log
{

    public enum LogDBType
    {
        Access,
        SQLServer
    }
    public class SystemLogData
    {
        public const string ADDTIME_Field = @"AddTime";
        public const string ID_Field = @"ID";
        public const string PRIMARYKEY_Field = @"ID";
        public const string LOGCONTENT_Field = @"LogContent";
        public const string LOGREMARK_Field = @"LogRemark";
        public const string LOGTYPE_Field = @"LogType";
        public const string OPERATOR_Field = @"Operator";
        private DateTime _AddTime;
        private Int32 _ID;
        private String _LogContent;
        private String _LogRemark;
        private string _LogType;
        private String _Operator;
        public static string DefaultOperator=string.Empty;
        public static LogDBType logDBType = LogDBType.SQLServer;        
        public DateTime AddTime
        {
            get { return _AddTime; }
            set { _AddTime = value; }
        }
        public Int32 ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
        public String LogContent
        {
            get { return _LogContent; }
            set { _LogContent = value; }
        }
        public String LogRemark
        {
            get { return _LogRemark; }
            set { _LogRemark = value; }
        }
        public string LogType
        {
            get { return _LogType; }
            set { _LogType = value; }
        }
        public String Operator
        {
            get { return _Operator; }
            set { _Operator = value; }
        }
        public SystemLogData()
        {
            _AddTime = DateTime.Now;
            _ID = 0;
            _LogContent = "";
            _LogRemark = "";
            _LogType = "";
            _Operator = "";
        }
        static public void SetConnectString(object logDS, string strConn)
        {
            if (logDBType == LogDBType.Access)
            {
                AccessLogDataSetTableAdapters.SystemLogTableAdapter DS=(AccessLogDataSetTableAdapters.SystemLogTableAdapter)logDS;
                OleDbConnection conn = DS.Connection;
                DS.Connection.ConnectionString = strConn;
            }            
        }
        private static string _DBConnectionString = string.Empty;
        public static string DBConnectionString
        {
            get { return _DBConnectionString; }
            set { _DBConnectionString = value; }
        }
        public void InserSQLLog()
        {
            if (logDBType == LogDBType.Access)
            {
                AccessLogDataSetTableAdapters.SystemLogTableAdapter DS = new AccessLogDataSetTableAdapters.SystemLogTableAdapter();
                if (!string.IsNullOrEmpty(_DBConnectionString))//不使用默认数据库连接字符串
                {
                    DS.Connection.ConnectionString = _DBConnectionString;
                }
                if (string.IsNullOrEmpty(_Operator))//使用默认操作员
                {
                    DS.Insert(_LogType,_LogContent,_LogRemark,DefaultOperator,_AddTime);
                }
                else
                {
                    DS.Insert(_LogType, _LogContent, _LogRemark, _Operator, _AddTime);
                }
            }
            else if (logDBType == LogDBType.SQLServer)
            {
                SQLSystemLogDataSetTableAdapters.SystemLogTableAdapter DS = new SQLSystemLogDataSetTableAdapters.SystemLogTableAdapter();
                if (!string.IsNullOrEmpty(_DBConnectionString))//不使用默认数据库连接字符串
                {
                    DS.Connection.ConnectionString = _DBConnectionString;
                }
                if (string.IsNullOrEmpty(_Operator))//使用默认操作员
                {
                    DS.Insert(_LogType, _LogContent, _LogRemark, DefaultOperator, _AddTime);
                }
                else
                {
                    DS.Insert(_LogType, _LogContent, _LogRemark, _Operator, _AddTime);
                }
            }
        }
    }
    public class SystemLogDataFactory
    {
        public static SystemLogData Construct(IDataReader reader)
        {
            SystemLogData data = new SystemLogData();

            data.AddTime = Convert.ToDateTime(reader["AddTime"]);

            data.ID = Convert.ToInt32(reader["ID"]);

            data.LogContent = Convert.ToString(reader["LogContent"]);

            data.LogRemark = Convert.ToString(reader["LogRemark"]);

            data.LogType = Convert.ToString(reader["LogType"]);

            data.Operator = Convert.ToString(reader["Operator"]);

            return data;
        }
        public static Int32 GetPrimaryKey(System.Data.DataTable dt)
        {
            return GetPrimaryKey(dt, 0);
        }
        public static Int32 GetPrimaryKey(System.Data.DataTable dt, int rowIndex)
        {
            Int32 PrimaryKey = Int32.MinValue;

            if (dt.Rows.Count < rowIndex + 1)

                return Int32.MinValue;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return Int32.MinValue;

            try { PrimaryKey = Convert.ToInt32(dr[SystemLogData.PRIMARYKEY_Field]); }
            catch { }

            return PrimaryKey;
        }
        public static SystemLogData Construct(System.Data.DataTable dt)
        {
            return Construct(dt, 0);
        }
        public static SystemLogData Construct(System.Data.DataTable dt, int rowIndex)
        {
            SystemLogData data = new SystemLogData();

            if (dt.Rows.Count < rowIndex + 1)

                return null;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return null;

            try { data.AddTime = Convert.ToDateTime(dr["AddTime"]); }
            catch { }

            try { data.ID = Convert.ToInt32(dr["ID"]); }
            catch { }

            try { data.LogContent = Convert.ToString(dr["LogContent"]); }
            catch { }

            try { data.LogRemark = Convert.ToString(dr["LogRemark"]); }
            catch { }

            try { data.LogType = Convert.ToString(dr["LogType"]); }
            catch { }

            try { data.Operator = Convert.ToString(dr["Operator"]); }
            catch { }

            return data;
        }
        public static SystemLogData Construct(DataGridView dgv, int rowIndex)
        {
            SystemLogData data = new SystemLogData();

            if (dgv.Rows.Count < rowIndex + 1)

                return null;

            DataGridViewCellCollection dr = dgv.Rows[rowIndex].Cells;

            if (dr == null)

                return null;

            for (int i = 0; i < dgv.Columns.Count; i++)
            {
                switch (dgv.Columns[i].DataPropertyName)
                {
                    case "AddTime":
                        try { data.AddTime = Convert.ToDateTime(dr[i].Value); }
                        catch { }
                        break;
                    case "ID":
                        try { data.ID = Convert.ToInt32(dr[i].Value); }
                        catch { }
                        break;
                    case "LogContent":
                        try { data.LogContent = Convert.ToString(dr[i].Value); }
                        catch { }
                        break;
                    case "LogRemark":
                        try { data.LogRemark = Convert.ToString(dr[i].Value); }
                        catch { }
                        break;
                    case "LogType":
                        try { data.LogType = Convert.ToString(dr[i].Value); }
                        catch { }
                        break;
                    case "Operator":
                        try { data.Operator = Convert.ToString(dr[i].Value); }
                        catch { }
                        break;
                }
            }
            return data;
        }
        public static Int32 GetPrimaryKey(DataGridView dgv, int rowIndex)
        {
            Int32 PrimaryKey = Int32.MinValue;

            if (dgv.Rows.Count < rowIndex + 1)

                return -1;

            DataGridViewCellCollection dr = dgv.Rows[rowIndex].Cells;

            if (dr == null)

                return Int32.MinValue;

            for (int i = 0; i < dgv.Columns.Count; i++)
            {
                if (dgv.Columns[i].DataPropertyName == SystemLogData.PRIMARYKEY_Field)
                {
                    try { PrimaryKey = Convert.ToInt32(dr[i].Value); }
                    catch { }
                    return PrimaryKey;
                }
            }
            return PrimaryKey;
        }
        public static void ExcelExport(DataGridView dgv)
        {
            if (dgv.Rows.Count > 65530)
            {
                MessageBox.Show("日志条目过多，无法导出，请重新选择索引条件");
                return;
            }
            Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
            try
            {
                app.Visible = false;
                Workbook wBook = app.Workbooks.Add(true);
                Worksheet wSheet = new Worksheet();
                wSheet = (Worksheet)wBook.Worksheets[1];
                wSheet.Name = "系统日志";
                wSheet.Cells[1, 1] = "编号";
                wSheet.Cells[1, 2] = "类型";
                wSheet.Cells[1, 3] = "内容";
                wSheet.Cells[1, 4] = "备注";
                wSheet.Cells[1, 5] = "操作员";
                wSheet.Cells[1, 6] = "时间";
                Range dr;
                dr = wSheet.get_Range("A1", "F1");
                dr.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DarkOrange);
                dr.Interior.Pattern = XlPattern.xlPatternSolid;
                for (int i = 0; i < dgv.Rows.Count; i++)
                {
                    SystemLogData log = Construct(dgv, i);
                    wSheet.Cells[2 + i, 1] = log.ID.ToString();
                    wSheet.Cells[2 + i, 2] = log.LogType;
                    wSheet.Cells[2 + i, 3] = log.LogContent;
                    wSheet.Cells[2 + i, 4] = log.LogRemark;
                    wSheet.Cells[2 + i, 5] = log.Operator;
                    wSheet.Cells[2 + i, 6] = log.AddTime.ToString();                    
                    if (i % 2 == 1)
                    {                        
                        dr= wSheet.get_Range("A" + (2 + i).ToString(), "F" + (2 + i).ToString());
                        dr.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGray);
                        dr.Interior.Pattern = XlPattern.xlPatternSolid;
                    }
                }
                dr = wSheet.get_Range("A1", "F" + (dgv.Rows.Count+1).ToString());
                dr.Columns.AutoFit();
                dr.HorizontalAlignment = XlHAlign.xlHAlignCenter;
                app.Visible = true;
                app.WindowState = XlWindowState.xlMaximized;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("导出Excel出错！错误原因" + ex.Message);
            }
        }
    }
}
